[user] CI1_main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "main.h"
  2. #include "usart.h"
  3. #include "gpio.h"
  4. #define ApplicationAddress 0x8010000
  5. #define VECT_TAB_OFFSET 0x10000
  6. int i = 0;
  7. int rf = 0;
  8. int row = 0;
  9. int data_count = 0;
  10. uint8_t msg[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x0D};
  11. uint8_t NACK[1]={0x79};
  12. uint32_t temp[1000];
  13. uint32_t FlashData1[1000];
  14. uint8_t FlashData[16];
  15. uint8_t com[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x0D};
  16. uint8_t com1[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x02, 0x0D};
  17. uint8_t start[7]={0xFF, 0xFF, 0x00, 0x00, 0x00, 0x03, 0x0D};
  18. void SystemClock_Config(void);
  19. void UserAppStart(void);
  20. void writeFlashTest(void);
  21. void printFlashTest(void);
  22. int main(void) {
  23. HAL_Init();
  24. SystemClock_Config();
  25. MX_GPIO_Init();
  26. MX_USART2_UART_Init;
  27. MX_UART4_Init;
  28. MX_USART1_UART_Init;
  29. HAL_UART_Transmit(&huart4,(uint8_t*)start,sizeof(start),10);
  30. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14, GPIO_PIN_SET);
  31. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  32. while (1) {
  33. if (rf == 0) {
  34. HAL_UART_Receive_IT(&huart4, (uint8_t *)msg,sizeof(msg));
  35. } else {
  36. HAL_UART_Receive_IT(&huart4, (uint8_t *)FlashData,sizeof(FlashData));
  37. }
  38. }
  39. return 0;
  40. }
  41. void SystemClock_Config(void) {
  42. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  43. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  44. __HAL_RCC_PWR_CLK_ENABLE();
  45. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  46. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  47. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  48. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  49. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  50. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  51. RCC_OscInitStruct.PLL.PLLM = 8;
  52. RCC_OscInitStruct.PLL.PLLN = 72;
  53. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  54. RCC_OscInitStruct.PLL.PLLQ = 3;
  55. if ((HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  56. Error_Handler();
  57. }
  58. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  59. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  60. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  61. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  62. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  63. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
  64. Error_Handler();
  65. }
  66. }
  67. void UserAppStart(void) {
  68. typedef void (*pFunction)(void);
  69. pFunction Jump_To_Application;
  70. uint32_t JumpAddress=0;
  71. if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000) {
  72. JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
  73. Jump_To_Application = (pFunction) JumpAddress;
  74. __set_MSP(*(__IO uint32_t*) ApplicationAddress);
  75. Jump_To_Application();
  76. }
  77. }
  78. void writeFlashTest(void) {
  79. FLASH_EraseInitTypeDef pEraseInit;
  80. HAL_FLASH_Unlock();
  81. pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
  82. pEraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  83. pEraseInit.Sector = FLASH_SECTOR_4;
  84. pEraseInit.NbSectors = 1;
  85. uint32_t PageError=0;
  86. HAL_FLASHEx_Erase(&pEraseInit,&PageError);
  87. for (i=1; i<(data_count * 4); i++) {
  88. HAL_FLASH_Program(TYPEPROGRAM_WORD,ApplicationAddress+(i*4),FlashData1[i]);
  89. }
  90. HAL_FLASH_Lock();
  91. }
  92. void printFlashTest(void) {
  93. for (i=1; i<(data_count * 4); i++) {
  94. temp[i] = *(__IO uint32_t*)(ApplicationAddress+(i*4));
  95. }
  96. }